﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Modules.Core;
using System.Windows.Controls;
using Modules.Library.Entities;
using System.Windows;
using System.Data;

namespace ComputerStoreManager.ViewModel {
    public class AddCustomerControlViewModel : BasePersonControlViewModel {

        #region Variables
        private TextBox txtBonusPoint;
        private TextBox txtDateOfRegister;
        private RadioButton cbbSex;
        private Button btnAddCustomer;
        private Button btnClearCustomer;

        #endregion

        #region ICommand
        public ICommand AddCustomerCommand {
            get;
            private set;
        }
        public ICommand ClearCommand {
            get;
            private set;
        }

        #endregion

        #region Init
        private void InitCmd() {
            AddCustomerCommand = new ExecutableCommand(DoAddNewPerson);
            ClearCommand = new ExecutableCommand(DoClearFields);
        }

        public override void InitControlInfo() {
            try
            {
                txtDateOfRegister.Text = DateTime.Now.ToString("dd/MM/yyyy");
                base.TxtPersonId.Text = Converter.ConvertToId(Model.GetThongSoPhu(KindOfObject.BY_CUSTOMER));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
        #endregion

        #region Methods
        public override void DoAddNewPerson()
        {
            try
            {
                if (CheckIsNotNull())
                {
                    Customer customer = new Customer();
                    //customer.AnhDaiDien = 
                    customer.DiemThuong = Convert.ToInt32(txtBonusPoint.Text);
                    customer.Email = base.TxtEmail.Text;
                    customer.DiaChi = base.TxtAddress.Text;
                    customer.HoVaTen = base.TxtPersonFullName.Text;
                    customer.MaKhachHang = base.TxtPersonId.Text;
                    customer.PassWord = base.TxtPassword.Password;
                    customer.SoCMND = base.TxtIdentifier.Text;
                    customer.SoDienThoai = base.TxtPhone.Text;
                    customer.UserName = base.TxtUserName.Text;
                    customer.GioiTinh = cbbSex.IsChecked == true ? "Nam" : "Nu";
                    customer.NgayDangKy = DateTime.Parse(Converter.ConvertToDateTime(txtDateOfRegister.Text, Converter.MMDDYYYY));
                    customer.NgaySinh = DateTime.Parse(Converter.ConvertToDateTime(base.TxtDateOfBirth.Text, Converter.MMDDYYYY));

                    if (!Model.InsertNewData(customer, KindOfObject.BY_CUSTOMER))
                    {
                        MessageBox.Show(Model.Error);
                    }
                    else
                    {
                        DoClearFields();
                        MessageBox.Show("Thành công");
                    }
                }
                else
                {
                    MessageBox.Show("Bạn không thể bỏ trống bất cứ trường nào", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }

        }
        public override void DoClearFields() {
            try
            {

                base.DoClearFields();
                this.txtBonusPoint.Text = string.Empty;
                this.txtDateOfRegister.Text = string.Empty;
                InitControlInfo();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        public override bool CheckIsNotNull() {
            if (this.txtBonusPoint.Text == string.Empty && this.txtDateOfRegister.Text == string.Empty && !base.CheckIsNotNull())
            {
                return false;
            }
            return true;
        }
        #endregion

        #region Constructor
        public AddCustomerControlViewModel(TextBox txtAddress, TextBox txtBonusPoint, TextBox txtCustomerId, TextBox txtCustomerName, TextBox txtDateOfBirth,
            TextBox txtDateOfRegister, TextBox txtEmail, TextBox txtIdentifier, PasswordBox txtPassword, TextBox txtPhone, TextBox txtUserName, RadioButton cbbSex,
            Button btnAddCustomer, Button btnClearCustomer)
            : base(txtCustomerName, txtCustomerId, txtDateOfBirth, txtIdentifier, txtPhone, txtAddress, txtEmail, txtUserName, txtPassword) {

            // TODO: Complete member initialization
            this.txtBonusPoint = txtBonusPoint;
            this.txtDateOfRegister = txtDateOfRegister;
            this.cbbSex = cbbSex;
            this.btnAddCustomer = btnAddCustomer;
            this.btnClearCustomer = btnClearCustomer;

            InitCmd();
            InitControlInfo();

        }
        #endregion

    }
}
